import 'package:flutter/material.dart';
import 'package:flutter_bilibili/core/extension/int_extension.dart';
import 'package:flutter_bilibili/core/model/video_model.dart';
import 'package:flutter_bilibili/ui/pages/video_play/video/video_play.dart';
import 'package:flutter_bilibili/ui/shared/app_theme.dart';
import 'package:flutter_bilibili/ui/shared/image_assets.dart';

import '../../../core/service/request/video_play_request.dart';
import '../../shared/math_compute.dart';

final _radius = 6.px;
final _iconSize = 14.px;

class HYHomeVideoItem extends StatefulWidget {
  final HYVideoModel video;

  const HYHomeVideoItem({required this.video, Key? key}) : super(key: key);

  @override
  State<HYHomeVideoItem> createState() => _HYHomeVideoItemState();
}

class _HYHomeVideoItemState extends State<HYHomeVideoItem> {
  @override
  Widget build(BuildContext context) {
    final _videoItem = widget.video;
    return GestureDetector(
      onTap: () {
        HYVideoRequestRequest.getMp4VideoPlayData(_videoItem.aid).then((value) {
          ///匹配字符串readyVideoUrl: 到readyDuration之间的字符串
          RegExp exp =
              RegExp(r'(?<=(readyVideoUrl: ))[\s\S]*?(?=(readyDuration))');
          // print("exp.allMatches(value)---${exp.stringMatch(value)}");
          String videoMp4 = exp.stringMatch(value) ?? "";
          // print("videoMp4-----------${videoMp4.substring(0,videoMp4.length)}");
          _videoItem.videoData = videoMp4.substring(1, videoMp4.length - 1);

          ///跳转至播放界面
          Navigator.of(context)
              .pushNamed(HYVideoPlayScreen.routeName, arguments: _videoItem);
        });
      },
      child: Stack(
        children: [
          Card(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(_radius),
                topRight: Radius.circular(_radius),
              ),
            ),
            child: Column(
              children: [
                Stack(
                  children: [
                    ///视频封面
                    buildHomeVideoItemCover(_videoItem),

                    /// 视频的评论数和观看数
                    buildHomeVideoItemInfo(_videoItem, context),

                    ///视频的时长
                    buildHomeVideoItemDuration(_videoItem.durationText)
                  ],
                ),

                /// 视频的标题
                buildHomeVideoItemTitle(context, _videoItem.title),
              ],
            ),
          ),

          ///视频Up主信息
          buildHomeVideoBottomInfo(context, _videoItem.owner.name),

          ///视频右下更多按钮
          buildHomeVideoMoreIcon()
        ],
      ),
    );
  }
}

///更多按钮
Widget buildHomeVideoMoreIcon() {
  return Positioned(
    child: Image.asset(
      ImageAssets.videoMoreCustomPNG,
      width: _iconSize,
      height: _iconSize,
    ),
    right: 8.px,
    bottom: 8.px,
  );
}

///视频封面
Widget buildHomeVideoItemCover(HYVideoModel video) {
  return ClipRRect(
    borderRadius: BorderRadius.only(
      topLeft: Radius.circular(_radius),
      topRight: Radius.circular(_radius),
    ),
    child: Image.network(
      video.pic,
      width: double.infinity,
      height: 110.px,
      fit: BoxFit.fill,
    ),
  );
}

///视频时长
Widget buildHomeVideoItemDuration(String duration) {
  return Positioned(
    right: 5.px,
    bottom: 3.px,
    child: Text(duration,
        style: TextStyle(
            color: Color.fromRGBO(255, 255, 255, 1),
            fontSize: HYAppTheme.xxSmallFontSize)),
  );
}

///视频播放量、评论数
Widget buildHomeVideoItemInfo(HYVideoModel video, BuildContext context) {
  int? _view = video.stat["view"];
  int? _remark = video.stat["danmaku"];
  String viewText = changeToWan(_view!);
  String _remarkText = changeToWan(_remark!);

  return Positioned(
    left: 5.px,
    bottom: 3.px,
    child: Text.rich(
      TextSpan(
        children: [
          WidgetSpan(
            alignment: PlaceholderAlignment.middle,
            child: Image.asset(
              ImageAssets.playCustomPNG,
              width: 12.px,
              height: 12.px,
            ),
          ),
          TextSpan(text: "  "),
          TextSpan(
              text: viewText,
              style: TextStyle(
                  color: Color.fromRGBO(255, 255, 255, 1),
                  fontSize: HYAppTheme.xxSmallFontSize)),
          TextSpan(text: "   "),
          WidgetSpan(
            alignment: PlaceholderAlignment.middle,
            child: Image.asset(
              ImageAssets.remarkPNG,
              width: 13.px,
              height: 13.px,
            ),
          ),
          TextSpan(text: "  "),
          TextSpan(
              text: _remarkText,
              style: TextStyle(
                  color: Color.fromRGBO(255, 255, 255, 1),
                  fontSize: HYAppTheme.xxSmallFontSize))
        ],
      ),
    ),
  );
}

///视频的标题
Widget buildHomeVideoItemTitle(BuildContext context, String videoTitle) {
  return Container(
    alignment: Alignment.topLeft,
    margin: EdgeInsets.symmetric(vertical: 8.px, horizontal: 8.px),
    child: Text(
      videoTitle,
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(color: Colors.black, fontSize: 13.px),
    ),
  );
}

///视频up主及id名称
Widget buildHomeVideoBottomInfo(BuildContext context, String info) {
  return Positioned(
    bottom: 8.px,
    left: 8.px,
    child: Row(
      children: [
        Image.asset(
          ImageAssets.uperCustomPNG,
          width: _iconSize,
          height: _iconSize,
        ),
        Container(
          margin: EdgeInsets.only(left: 6.px),
          child: Text(info,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: HYAppTheme.xxSmallFontSize,
                color: Color.fromRGBO(149, 149, 149, 1),
              )),
        ),
      ],
    ),
  );
}
